#include<stdlib.h>
#include<stdio.h>
#include<getopt.h>
#include<string.h>

void usage(const char *p);

int main(int argc, char *argv[])
{
    int ch=0;
    opterr=0; // prevent error information to print for unrecognized options
    while( (ch=getopt(argc, argv, "i:ao:vh") ) != -1 )
     {
        switch(ch)
        {
           case 'i':
                printf("option i: %s\n", optarg);
                printf("optind=%d\n\n", optind);
                break;
           case 'a':
                printf("option a :%c\n", ch);
                printf("optind=%d\n\n", optind);
                break;
           case 'o':
                printf("option o: %s\n", optarg);
                printf("optind=%d\n\n", optind);
                break;
           case 'v':
                printf("option v: %c\n", ch);
                printf("optind=%d\n\n", optind);
                break;
           case 'h':
                usage(argv[0]);
                printf("optind=%d\n\n", optind);
                break;
           default:
                printf("unrecognized option: %c\n", optopt);
                usage(argv[0]);
        }
     }
     /*
    if ( optind < argc )
       {
          printf("\nnon-option arguments below:\n");
          while( optind < argc )
            {
                printf("%s\n", argv[optind++]);
                printf("optind=%d\n\n", optind-1);
            }
       }
       */
       printf("optind=%d\n\n", optind);
       printf("optorg=%s\n\n", argv[optind]);

    return 0;
}
void usage(const char *p)
{
   printf("Usage: %s [-i infile] [-a] [-o outfile] [-v] [-h] [file]\n", p);
}
